<?php
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
/*
	[天生创想] (C)2008-2011 515158 Inc.
	This is NOT a freeware, use is subject to license terms
    URL:http://www.515158.com/
*/

get_config_keytable("config_user");
empty($do) && $do = 'list';
if ($do == 'list') {
	//列表信息 
	$wheresql = '';
	$page = max(1, getGP('page','G','int'));
	$pagesize = 15;
	$offset = ($page - 1) * $pagesize;
	$url = 'admin.php?ac=index&fileurl=user';

	if ($keyword = getGP('keyword','G')) {
		$wheresql .= " AND (b.name LIKE '%$keyword%' OR a.username LIKE '%$keyword%')";
		$url .= '&keyword='.rawurlencode($keyword);
	}
	if ($department = getGP('department','G','int')) {
		$wheresql .= " AND a.departmentid = $department";
		$url .= '&department='.$department;	
	}
	if ($usergroup = getGP('usergroup','G','int')) {
		$wheresql .= " AND a.groupid = $usergroup";
		$url .= '&usergroup='.$usergroup;	
	}
	
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b WHERE a.id=b.uid $wheresql");
     $sql = "SELECT * FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b WHERE a.id=b.uid $wheresql ORDER BY a.id ASC LIMIT $offset, $pagesize";
	$result = $db->fetch_all($sql);

	include_once('template/index.php');

} elseif ($do == '删 除') {
get_config_keytable("config_user_delete");
	$idarr = getGP('id','P','array');
	foreach ($idarr as $id) {
	$db->query("DELETE FROM ".DB_TABLEPRE."user WHERE id = '$id'");
	$db->query("DELETE FROM ".DB_TABLEPRE."user_view WHERE uid = '$id'");
	if($id!='')
	{
   $oalog = array(
		'uid' => $_USER->id,
		'content' => $id,
		'title' => '删除用户信息'.$id,
		'startdate' => get_date('Y-m-d H:i:s',PHP_TIME),
		'contentid' => $id,
		'type' => '3'
	);
	insert_db('oalog',$oalog);
	}	
	}
	
	goto_page('admin.php?ac=index&fileurl=user&message=1');

} elseif ($do == '导 出') {
//导出excel
//生成格式
$datename="document".get_date('Y',PHP_TIME).get_date('m',PHP_TIME).get_date('d',PHP_TIME).get_date('H',PHP_TIME).get_date('i',PHP_TIME).get_date('s',PHP_TIME);
$outputFileName = 'excel/'.$datename.'.xls';
//生成数据
    $content = array();
	$archive=array("用户名","权限组","姓名","岗位","状态","允许登录IP","所属部门");
	$content[] = $archive;
	$wheresql = '';
	//根据条件导出
	if ($keyword = getGP('keyword','P')) {
		$wheresql .= " AND (b.name LIKE '%$keyword%' OR a.username LIKE '%$keyword%')";
		
	}
	if ($department = getGP('department','P','int')) {
		$wheresql .= " AND a.departmentid = $department";
		
	}
	if ($usergroup = getGP('usergroup','P','int')) {
		$wheresql .= " AND a.groupid = $usergroup";
			
	}
	//echo getGP('keyword','P');
	//exit;
	//SQL查询要导出的内容
	$sql = "SELECT * FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b WHERE a.id=b.uid $wheresql ORDER BY a.id ASC";
	$result = $db->query($sql);
	while ($row = $db->fetch_array($result)) {	
	if($row['ischeck']=='1'){
$ischeck='正常';
}else{
$ischeck='禁用';
}
	//将数据传递给数组
	$archive = array("".$row[username]."","".get_inc_usergroup_name($row['groupid'])."","".$row[name]."","".get_inc_positionid_name($row['positionid'])."","".$ischeck."","".$row[loginip]."","".get_inc_department_name($row['departmentid'])."");
	//初使化数组数据
	$content[] = $archive;
	}
//$myArr=$content;
$excel = new ExcelWriter($outputFileName);
if($excel==false) 
echo $excel->error; 
foreach($content as $v){
$excel->writeLine($v);
}
$excel->sendfile($outputFileName);
} 


?>